package com.heima.article.test;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(classes = ArticleApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class ArticleFreemarkerTest
{

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Test
    public void createStaticUrlTest() throws Exception {
        // 1.获取文章内容
        ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers
                .<ApArticleContent>lambdaQuery()
                .eq(ApArticleContent::getArticleId, 1383828014629179393L));
        if (apArticleContent == null || StringUtils.isBlank(apArticleContent.getContent())) {
            log.error("文章内容不存在！");
            return;
        }

        // 2.文章内容通过freemarker生成html文件
        StringWriter out = new StringWriter();
        Template template = configuration.getTemplate("article.ftl");

        Map<String, Object> params = new HashMap<>();
        params.put("content", JSONArray.parseArray(apArticleContent.getContent()));

        template.process(params, out);
        InputStream is = new ByteArrayInputStream(out
                .toString()
                .getBytes());

        // 3.把html文件上传到minio中
        String path = fileStorageService.uploadHtmlFile("", apArticleContent.getArticleId() + ".html", is);

        // 4.修改ap_article表，保存static_url字段
        ApArticle article = new ApArticle();
        article.setId(apArticleContent.getArticleId());
        article.setStaticUrl(path);
        apArticleMapper.updateById(article);

    }
}
